Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parseISO not returning Invalid Date when there are space(s) in passed string #1791

Merged
merged 6 commits into from Jul 17, 2020

Conversation

imballinst
Copy link
Contributor

Fixes #1748. This PR adds additional validation when a datetime string is split using the existing RegEx in splitDateString, hence parseISO will return "Invalid Date" for strings with space(s).

Current

// Since we only check first 2 array elements, it won't resolve to "Invalid Date".
splitDateString('2014-02-11T07:00:00  basketball'); // ['2014-02-11', '07:00:00', 'basketball']
parseISO('2014-02-1107:00:00  basketball'); // 2014-02-11T00:00:00.000Z (I'm in GMT+7)

// This is also the same for date-only string.
splitDateString('2014-02-11  basketball'); // ['2014-02-11', '', 'basketball']
parseISO('2014-02-11  basketball'); // 2014-02-10T17:00:00.000Z (if time is not specified, we use 12am)

This PR

With that, the solution of this PR is to always check the resulting array length from splitDateString. If the array length is more than 2, then it contains space(s) and it should resolve to "Invalid Date".

This is also considering the fact that ISO8601-formatted dates don't have spaces in it (source: https://www.w3.org/TR/NOTE-datetime).

splitDateString('2014-02-11T07:00:00  basketball'); // ['2014-02-11', '07:00:00', 'basketball']
parseISO('2014-02-11T07:00:00  basketball'); // Invalid Date

splitDateString('2014-02-11  basketball'); // ['2014-02-11', '', 'basketball']
parseISO('2014-02-11  basketball'); // Invalid Date

Signed-off-by: Try Ajitiono ballinst@gmail.com

Signed-off-by: Try Ajitiono <ballinst@gmail.com>
Signed-off-by: Try Ajitiono <ballinst@gmail.com>
Signed-off-by: Try Ajitiono <ballinst@gmail.com>
@imballinst imballinst self-assigned this May 20, 2020
Signed-off-by: Try Ajitiono <ballinst@gmail.com>
Signed-off-by: Try Ajitiono <ballinst@gmail.com>
Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it makes sense!

src/parseISO/index.js Outdated Show resolved Hide resolved
@kossnocorp kossnocorp merged commit cff48aa into date-fns:master Jul 17, 2020
@imballinst imballinst deleted the fix-parseiso branch August 3, 2020 10:47
@tan75 tan75 mentioned this pull request Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parseISO - parsing invalid string into a valid date
2 participants